package com.example.factory;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {

    private Button get;
    private TextView wendu;
    private TextView shidu;
    private TextView time;
    private static final int COMPLETED = 0;
    private Handler handler = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        get = (Button) findViewById(R.id.get);
        wendu = (TextView) findViewById(R.id.wendu);
        shidu = (TextView) findViewById(R.id.shidu);
        time = (TextView) findViewById(R.id.time);


        //请求数据
        get.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder().url("http://192.168.86.114/data").build();
                client.newCall(request).enqueue(new okhttp3.Callback() {

                    @Override
                    public void onFailure(Call call, IOException e) {
                        //e.printStackTrace();
                        System.out.println("获取数据失败");
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {


                        String json = response.body().string();
                        System.out.println(json);
                        //String字符串转换为Json对象
                        JSONObject jobj = JSON.parseObject(json);
                        JSONObject info = jobj.getJSONObject("info");

                        //遍历每一个json对象，获取传感器数据
                        final String wendu1 = jobj.getString("wendu");
                        final String shidu1 = jobj.getString("shidu");

                        String ip1  = info.getString("ip");
                        final String time1 = info.getString("time");
                        String equipment1 = info.getString("equipment");


                        Handler handler=new Handler(Looper.getMainLooper());
                        handler.post(new Runnable(){
                            public void run(){
                                wendu.setText(wendu1);
                                shidu.setText(shidu1);
                                time.setText(time1);

                            }
                        });

                        //将温度与人体的舒适度温度进行比较
                        if(wendu1.compareTo("30.0")>0){
                            Handler handler1=new Handler(Looper.getMainLooper());
                            handler1.post(new Runnable(){
                                public void run(){
                                    Toast.makeText(MainActivity.this,"今天天气有点热，防止中暑欧",Toast.LENGTH_SHORT).show();
                                }
                            });
                        }
                        if(wendu1.compareTo("29.0")<0&&wendu1.compareTo("20.0")>0){
                            Handler handler2=new Handler(Looper.getMainLooper());
                            handler2.post(new Runnable(){
                                public void run(){
                                    Toast.makeText(MainActivity.this,"今天的工作环境适宜！",Toast.LENGTH_SHORT).show();
                                }
                            });
                        }
                        if(wendu1.compareTo("10.0")<0){
                            Handler handler3=new Handler(Looper.getMainLooper());
                            handler3.post(new Runnable(){
                                public void run(){
                                    Toast.makeText(MainActivity.this,"今天天气比较冷，建议提高车间的温度，让工人保暖！",Toast.LENGTH_SHORT).show();
                                }
                            });
                        }

                    }
                });

            }
        });

    }
}
